﻿using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;

namespace TestConsoleApp
{
    public class TestRedis
    {
        static readonly ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(
            new ConfigurationOptions
            {
                EndPoints = { "localhost:6379" }
            });


        public static void Test()
        {


            Console.WriteLine(Encoding.Default);


            //var list = Encoding.GetEncodings();

            //var resut = db.StringSet(new RedisKey("qx"), new RedisValue("123"));

            //Console.WriteLine(resut);


            //var result1 = db.StringGet(new RedisKey("qx"));

            //var result1 = GetItem<string>("qx2");

            //Console.WriteLine("qx2:" + result1);

            string str = "钱骁";


            var result2 = GetDB().StringGet(new RedisKey(str));
            if (result2.HasValue)
                Console.WriteLine(str + ":" + result2);
            string path = "image/news6.ico";

            byte[] tempbuffer = null;
            string str1 = null;

            using (FileStream fileStream = File.OpenRead(path))
            {
                tempbuffer = new byte[fileStream.Length];
                fileStream.Read(tempbuffer);
                Console.WriteLine("Set:qx:image:1---" + tempbuffer);
                str1 = System.Text.Encoding.Default.GetString(tempbuffer);
                GetDB().StringSet("qx:image:1", tempbuffer);
            }

            path = "image/news9.ico";
            using (FileStream fileStream = File.Create(path))
            {
                //str1= GetItem<string>("qx:image:1");

                //tempbuffer = System.Text.Encoding.Default.GetBytes(str1);

              tempbuffer =  GetDB().StringGet("qx:image:1");
                Console.WriteLine("Get:qx:image:1---" + tempbuffer);
                fileStream.Write(tempbuffer);
            }
        }


        public static IDatabase GetDB()
        {
            return redis.GetDatabase();
        }

        public static void SetItem<T>(string key, T value)
        {

            IDatabase redDb = GetDB();
            redDb.StringSet(key, ToByteArray<T>(value));
        }

        public static T GetItem<T>(string key)
        {
            IDatabase redDb = GetDB();
            RedisValue redisResult = redDb.StringGet(key);
            T objResult = FromByteArray<T>(redisResult);
            return objResult;
        }

        public static byte[] ToByteArray<T>(T obj)
        {
            if (obj == null)
                return null;
            BinaryFormatter bf = new BinaryFormatter();
            using (MemoryStream ms = new MemoryStream())
            {
                bf.Serialize(ms, obj);
                return ms.ToArray();
            }
        }

        public static T FromByteArray<T>(byte[] data)
        {
            if (data == null)
                return default(T);
            BinaryFormatter bf = new BinaryFormatter();
            using (MemoryStream ms = new MemoryStream(data))
            {
                object obj = bf.Deserialize(ms);
                return (T)obj;
            }
        }

    }
}
